<script setup lang="ts">
import { onMounted, reactive, ref, watch } from "vue";
import type { FormInstance, FormRules } from "element-plus";
import { useUserStore } from "@/store/modules/user";


const props = defineProps({
  showInfoDialog: Boolean,  // 控制弹框的显示与隐藏
  // 父组件传过来的表单数据
  formData: {
    type: Object,
  },
  row: {
    type: Object
  }
});
const emit = defineEmits(["closeDialog"]);

const ruleFormRef = ref<FormInstance>();

let ruleForm = reactive({
  type: 0,
  name: '', // 名称
  price: "", // 单价
  inputNum: "", // 数量
  unit: "", // 单位
  goodsInfo: "", // 备注
  inputPerson: useUserStore().userInfo.id
});

const rules = reactive<FormRules>({
  type: [{ required: true, message: "请选择物资类型", trigger: "change" }],
  name: [{ required: true, message: "请输入物资名称", trigger: "change" }],
  price: [{ required: true, message: "请输入物资单价", trigger: "change" }],
  inputNum: [{ required: true, message: "请输入入库数量", trigger: "change" }],
  unit: [{ required: true, message: "请输入入库单位", trigger: "change" }],
  goodsInfo: [{ message: "可输入该物资的一些入库备注信息", trigger: "change" }],
});

/**
 * 提交表单
 */
const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  await formEl.validate((valid, fields) => {
    if (valid) {
      console.log("submit!");
    } else {
      console.log("error submit!", fields);
    }
  });
};


/**
 * 重置表单
 */
const resetForm = (formEl: FormInstance | undefined) => {
  /** 此处会触发两次，待优化----------------------------------------------------------------------------------------------------------------------------- */
  // 关闭弹框
  emit("closeDialog");
  // 重置验证规则
  if (!formEl) return;
  formEl.resetFields();
};


const typeOptions = [
  {
    value: 0,
    label: '领用物资'
  },
  {
    value: 1,
    label: '借用物资'
  }
]
onMounted(() => {
  console.log(props.formData)
  // ruleForm = props.formData
})
watch(
  () => props.formData,
  () => {
    console.log(props.formData)
  },

  { immediate: true }
);
</script>

<template>
  <div class="el-dialog-25">
    <el-dialog v-model="props.showInfoDialog" :title="props.formData ? '编辑物资' : '新增物资'" @close="resetForm(ruleFormRef)">
      <el-form ref="ruleFormRef" :model="ruleForm" :rules="rules" label-width="120px" class="demo-ruleForm" status-icon>
        <el-form-item label="物资类型：" prop="type" class="el-form-item-full">
          <el-select v-model="ruleForm.type" class="m-2" placeholder="请选择物资类型">
            <el-option v-for="item in typeOptions" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="物资名称：" prop="name">
          <el-input v-model="ruleForm.name" placeholder="请输入物资名称" clearable />
        </el-form-item>
        <el-form-item label="物资单价：" prop="price">
          <el-input v-model="ruleForm.price" placeholder="请输入物资单价" clearable />
        </el-form-item>
        <el-form-item label="入库数量：" prop="inputNum">
          <el-input v-model="ruleForm.inputNum" placeholder="请输入入库数量" clearable />
        </el-form-item>
        <el-form-item label="入库单位：" prop="unit">
          <el-input v-model="ruleForm.unit" placeholder="请输入入库单位" clearable />
        </el-form-item>
        <!-- <el-form-item label="临界值：" prop="criticalValue">
          <el-input
            v-model="ruleForm.criticalValue"
            placeholder="请输入临界值"
            clearable
          />
        </el-form-item> -->
        <el-form-item label="入库备注：" prop="goodsInfo">
          <el-input v-model="ruleForm.goodsInfo" maxlength="100" show-word-limit type="textarea"
            placeholder="可输入该物资的一些入库备注信息" clearable />
        </el-form-item>
        <el-form-item>
          <el-button @click="resetForm(ruleFormRef)" class="btn-white">取消</el-button>
          <el-button type="primary" @click="submitForm(ruleFormRef)" class="btn-green">
            确定
          </el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<style scoped lang="scss"></style>
